﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;

namespace MDT.LifeSewagePlatform.Repository
{
    public class CompanyRepository: HandleBaseRepository<CompanyEntity>,ICompanyRepository
    {
        public IQueryable<CompanyView> GetCompanies(Expression<Func<CompanyView,bool>> expression)
        {
            var query = from company in _dbContext.Company.AsNoTracking()
                        join province in _dbContext.Regions.AsNoTracking() on company.ProvinceID equals province.ID
                        join city in _dbContext.Regions.AsNoTracking() on company.CityID equals city.ID
                        join area in _dbContext.Regions.AsNoTracking() on company.AreaID equals area.ID
                        join type in _dbContext.Details.AsNoTracking() on company.PlaceTypeID equals type.ID
                        select new CompanyView()
                        {
                            ID = company.ID,
                            CompanyName = company.CompanyName,
                            CityName = city.DisplayName,
                            CityID = city.ID,
                            ProvinceID = province.ID,
                            ProvinceName = province.DisplayName,
                            AreaID = area.ID,
                            AreaName = area.DisplayName,
                            PlaceTypeID = type.ID,
                            PlaceTypeName = type.RowName,
                        };
            return query.Where(expression);
        }

        public IQueryable<KeyValueView<int>> GetCompanyList(Expression<Func<KeyValueView<int>, bool>> expression)
        {
            var query = from company in _dbContext.Company.AsNoTracking()
                        select new KeyValueView<int>()
                        {
                          Id= company.ID,
                          DisplayName=company.CompanyName,
                          Description=company.CityID.ToString()
                        };
            return query.Where(expression);
        }
    }
}
